package com.xuanmiao.smsback.service.email.event;

import com.xuanmiao.smsback.service.email.MailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SendEmailEventListener implements ApplicationListener<SendEmailEvent> {

    @Autowired
    @Qualifier("mailServiceImpl")
    private MailService mailService;

    @Override
    public void onApplicationEvent(SendEmailEvent event) {

        SendEmailObject eventObject = event.getObject();

        log.info("发送邮件"+eventObject.getTo()+"----"+eventObject.getSubject()+"-----"+eventObject.getContent());

        if(CollectionUtils.isNotEmpty(eventObject.getTo())){
            for(String emailAddress : eventObject.getTo()){
                mailService.sendHtmlMail(emailAddress,eventObject.getSubject(),eventObject.getContent());
            }
        }

    }
}
